//package com.night.gateway.filter;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.night.common.entity.ResultVo;
//import com.night.common.entity.StatusCode;
//import com.night.gateway.jwtUtil.JwtUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
///**
// * @author rzh6230
// * 2022/10/1 10:45
// */
//@Component
//public class LoginFilter implements GlobalFilter, Ordered {
//    @Autowired
//    JwtUtils jwtUtils;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        System.out.println("进入LoginFilter");
//        ServerHttpRequest request = exchange.getRequest();
//        if (!"/api/login".equals(request.getURI().getPath())) {
//            if (getToken(request.getHeaders().get("jwt"))) {
//                List<String> list = request.getHeaders().get("jwt");
//                assert list != null;
//                if (jwtUtils.verifyJWT(list.get(0))) {
//                    return chain.filter(exchange);
//                }else {
//                   return exchange.getResponse().writeWith(Mono.just(result(exchange,StatusCode.TOKEN_FAIL)));
//                }
//            }
//            System.out.println("拦截");
//            return exchange.getResponse().writeWith(Mono.just(result(exchange,StatusCode.NO_SESSION)));
//        }
//        System.out.println("放行");
//        return chain.filter(exchange);
//    }
//
//    public boolean getToken(List<String> token) {
//        if (token == null) return false;
//        return token.size() != 0;
//    }
//
//    public DataBuffer result(ServerWebExchange exchange,StatusCode statusCode) {
//        ServerHttpResponse response = exchange.getResponse();
//        ResultVo<Object> resultVo = new ResultVo<>(statusCode);
//        byte[] bytes = null;
//        try {
//            bytes = new ObjectMapper()
//                    .writeValueAsString(resultVo).getBytes(StandardCharsets.UTF_8);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
//        return dataBuffer;
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}
